package com.xuzao.developer.service.impl;

import com.google.common.collect.Lists;
import com.xuzao.developer.base.model.SysUser;
import com.xuzao.developer.dao.SysUserDao;
import com.xuzao.developer.dto.SysUserDTO;
import com.xuzao.developer.enums.RoleTypeEnum;
import com.xuzao.developer.enums.WorkTypeEnum;
import com.xuzao.developer.service.SysUserService;
import com.xuzao.developer.vo.BaseResultVo;
import com.xuzao.developer.vo.SysUserVO;
import com.xuzao.developer.tools.entity.Pager;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author xuzao
 * @description
 * @date 2019-08-22 11:37:49
 **/
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Override
    public List<SysUserVO> list(SysUserDTO dto, Pager pager) {
        List<SysUserVO> sysUserVOS = Lists.newArrayList();
        List<SysUser> sysUsers = sysUserDao.list(dto, pager).getList();
        if (!sysUsers.isEmpty()) {
            sysUsers.forEach(o -> {
                sysUserVOS.add(createVO(o));
            });
        }
        return sysUserVOS;
    }


    public SysUserVO createVO(SysUser sysUser) {
        SysUserVO sysUserVO = new SysUserVO();
        BeanUtils.copyProperties(sysUser, sysUserVO);
        if (sysUser.getRoleId() != null) {
            sysUserVO.setShowRole(RoleTypeEnum.valueOf(sysUser.getRoleId().intValue()).getDesc());
        }
        if (sysUser.getWorkType() != null) {
            sysUserVO.setShowWork(WorkTypeEnum.valueOf(sysUser.getWorkType()).getDesc());
        }
        return sysUserVO;
    }


    @Override
    public BaseResultVo insert(SysUserDTO dto) {
        SysUser sysUser = new SysUser();
        //TODO 赋值
        BeanUtils.copyProperties(dto,sysUser);
        sysUser.setRoleId(Long.valueOf(RoleTypeEnum.USER.getCode()));
        sysUser.setPassWord(DigestUtils.md2Hex("123456"));
        Integer count = sysUserDao.insertSelective(sysUser);
        if (count > 0) {
            return BaseResultVo.success();
        }
        return BaseResultVo.error();

    }

    @Override
    public BaseResultVo update(SysUserDTO dto) {
        SysUser sysUser = new SysUser();
        //TODO 赋值
        sysUser.setRealName(dto.getRealName());
        sysUser.setUserName(dto.getUserName());
        sysUser.setWorkType(dto.getWorkType());
        sysUser.setId(dto.getId());
        sysUser.setPassWord(DigestUtils.md2Hex(dto.getPassWord()));
        Integer count = sysUserDao.updateSelectiveById(sysUser);
        if (count > 0) {
            return BaseResultVo.success();
        }
        return BaseResultVo.error();
    }

    @Override
    public SysUserVO get(Long id) {
        return createVO(sysUserDao.selectById(id));
    }

    @Override
    public void del(Long id) {
        sysUserDao.deleteById(id);
    }
}